﻿#set($PKColumnName="")
#foreach($u in $TableInfos)
#beforeall
#each
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK=="True")
#set($PKColumnName=  $c.ColumnName)
#end
#end
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.${u.TabName}
{
    public partial class edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
       
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
           
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.${u.TabName}().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }

        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.${u.TabName} bll = new BLL.${u.TabName}();
            Model.${u.TabName} model = bll.GetModel(_id);
#foreach($c in $u.ColumnInfos)  
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit"  || $c.TypeName=="tinyint")
            ddl${c.ColumnName}.SelectedValue = model.${c.ColumnName}.ToString();
#elseif($c.TypeName=="datetime")
            txt${c.ColumnName}.Text =(model.${c.ColumnName} != null ? ((DateTime)model.${c.ColumnName}).ToString("yyyy-MM-dd") : ""); 
#elseif($c.TypeName=="int")
            txt${c.ColumnName}.Text = model.${c.ColumnName}.ToString();
#else
            txt${c.ColumnName}.Text = model.${c.ColumnName};
#end
#end
#end 
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = true;
            Model.${u.TabName} model = new Model.${u.TabName}();
            BLL.${u.TabName} bll = new BLL.${u.TabName}();
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit"  || $c.TypeName=="tinyint")
            model.${c.ColumnName} = int.Parse(ddl${c.ColumnName}.SelectedValue);
#elseif($c.TypeName=="datetime")
            model.${c.ColumnName} = ${c.TypeCSharpName}.Parse(txt${c.ColumnName}.Text); 
#elseif($c.TypeName=="int")
            model.${c.ColumnName} = ${c.TypeCSharpName}.Parse(txt${c.ColumnName}.Text); 
#else
            model.${c.ColumnName} = txt${c.ColumnName}.Text;
#end
#end
#end
            if (bll.Add(model) < 1)
            {
                result = false;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = true;
            BLL.${u.TabName} bll = new BLL.${u.TabName}();
            Model.${u.TabName} model = bll.GetModel(_id);
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit"  || $c.TypeName=="tinyint")
            model.${c.ColumnName} = int.Parse(ddl${c.ColumnName}.SelectedValue);
#elseif($c.TypeName=="datetime")
            model.${c.ColumnName} = ${c.TypeCSharpName}.Parse(txt${c.ColumnName}.Text); 
#elseif($c.TypeName=="int")
            model.${c.ColumnName} = ${c.TypeCSharpName}.Parse(txt${c.ColumnName}.Text); 
#else
            model.${c.ColumnName} = txt${c.ColumnName}.Text;
#end
#end
#end
            if (!bll.Update(model))
            {
                result = false;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("${u.TabName}", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！", "", "Error");
                    return;
                }
                JscriptMsg("修改${u.TabCaption}成功啦！", "list.aspx", "Success");
            }
            else //添加
            {
                ChkAdminLevel("${u.TabName}", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦！", "", "Error");
                    return;
                }
                JscriptMsg("添加${u.TabCaption}成功啦！", "list.aspx", "Success");
            }
        }


    }
}
#end